/*
 * @Descripttion: 
 * @version: 
 * @Author: yuanliutech
 * @Date: 2022-11-01 14:23:34
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2023-02-08 15:17:16
 */
const app = getApp()
import {
    showMsg
} from '../../../../utils/config.js'
import {
    reqDoorList,
    reqInfraredList,
    reqCommand,
    reqBatchDoorSwitch,
    reqFloorById,
    reqDeviceList,
    reqDeviceUserpage
} from '../../../../request/api'
Page({
    data: {
        openDoor: false, //开门告警
        closeDoor: false, //关门告警
        height: '480rpx',
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        deviceInfo: {},
        dataList: [],
        page: 1,
        isNextPage: true, //是否下一页
        triggered: false, //是否下拉刷新列表
        srcFraredActive: '/statics/images/device/infrared_active.png',
        srcInfraredActive: '/statics/images/device/infrared_inactive.png',
        show: false,
        showStatus: false,
        showStatus2: false,
        serviceTypeList: [{
            id: 'work_mode',
            text: '工作模式',
        }, ],
        serviceTypeList2: [{
            id: 'muffling_cmd',
            text: '消音下发',
        }, {
            id: 'temperature_throshold',
            text: '温度阈值设置',
        }, {
            id: 'heartbeat_time',
            text: '心跳周期设置',
        }, ],
        serviceValueList: [{
            id: 0,
            text: '普通模式',
        }, {
            id: 1,
            text: '实时上报模式',
        }, {
            id: 2,
            text: '活动量统计模式',
        }, {
            id: 3,
            text: '建行模式',
        }, {
            id: 4,
            text: '三小场所模式',
        }, {
            id: 5,
            text: '老人独居模式',
        }, ],
        serviceValueList2: [{
            id: 1,
            text: '消音',
        }, ],
        serviceType: "work_mode",
        serviceValue: "0",
        serviceTypename: "工作模式",
        serviceValuename: "普通模式",
        isProprietor:false,
    },
    onShow() {
        this.setData({
            isProprietor:app.globalData.isProprietor
        })
    },
    onLoad(e) {
        console.log(JSON.parse(e.info));
        let item = JSON.parse(e.info)

        this.getDeviceInfo(e)
        if (item.deviceType == 'DoorMagnetism') {
            this.setData({
                height: '480rpx'
            })
        } else {
            this.setData({
                height: '350rpx'
            })
        }
        if (item.openDoor == 0) {
            this.setData({
                openDoor: false
            })
        } else {
            this.setData({
                openDoor: true
            })
        }
        if (item.closeDoor == 0) {
            this.setData({
                closeDoor: false
            })
        } else {
            this.setData({
                closeDoor: true
            })
        }
        // this.setData({
        //     serviceValue:this.data.deviceInfo.workMode
        // })
    },
    // 平面图
    async addbind() {
        const res = await reqFloorById(this.data.deviceInfo.spaceId)
        if (res.code !== 20000) return showMsg(res.message)
        console.log(res);
        if (res.data.rows.image == '') return showMsg('当前楼层暂无平面图，请上传后再标记')
        wx.navigateTo({
            url: `../ichnography/ichnography?id=${this.data.deviceInfo.id}&mark=${this.data.deviceInfo.mark}&image=${res.data.rows.image}`,
        })
    },
    async onChangeopenDoor(event) {
        this.setData({
            openDoor: event.detail,
        });
        console.log(event.detail);
        const res = await reqBatchDoorSwitch({
            idList: [this.data.deviceInfo.id],
            openDoor: event.detail ? 1 : 0,
            closeDoor: this.data.closeDoor ? 1 : 0
        })
        if (res.code !== 20000) return showMsg(res.message)
    },
    async onChangecloseDoor(event) {
        this.setData({
            closeDoor: event.detail,
        });
        console.log(this.data.openDoor);
        console.log(event.detail);
        const res = await reqBatchDoorSwitch({
            idList: [this.data.deviceInfo.id],
            openDoor: this.data.openDoor ? 1 : 0,
            closeDoor: event.detail ? 1 : 0
        })
        if (res.code !== 20000) return showMsg(res.message)
    },
    // 只能输入数字 0-100
    handleInput(e) {
        let serviceValue = e.detail.value.replace(/\D/g, '')
        if (serviceValue > 100) {
            serviceValue = 100
        }
        this.setData({
            serviceValue
        })
    },
    // 只能输入数字 1-100
    handleInput2(e) {
        let serviceValue = e.detail.value.replace(/\D/g, '')
        // console.log(serviceValue);
        if (serviceValue === '0') {
            serviceValue = 1
        }
        if (serviceValue > 100) {
            serviceValue = 100
        }
        this.setData({
            serviceValue
        })
    },
    // 获取imei
    getDeviceInfo(e) {
        let item = JSON.parse(e.info)
        if(app.globalData.isProprietor){
            // 业主
            this.getDeviceUserpage(item.imei)
        }else{
            // 装维
            this.getDeviceList(item.imei)
        }
    },
    // 业主设备列表
    async getDeviceUserpage(imei){
        const res = await reqDeviceUserpage(1, 10, {
            userId:app.globalData.userId,
            imei:imei
        })
        if (res.code !== 20000) return showMsg(res.message)
        console.log(res);
        let {
            rows
        } = res.data
        this.setData({
            deviceInfo: rows[0]
        })
        this.getDataList()
    },
    /**
     * @name: 获取设备列表
     * @msg: *parkId：园区id  online:在离线筛选   productId:产品筛选
     * @return {*}
     */
    async getDeviceList(imei) {
        let params = {
            orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
            online: '',
            productId: '',
            spaceId: '',
            imei: imei,
            minBattery: '',
            maxBattery: '',
            isAlarm: '',
            userId:app.globalData.userId,
            roleName:app.globalData.rolename,
        }
        const res = await reqDeviceList(1, 10, params)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        this.setData({
            deviceInfo: rows[0]
        })
        this.getDataList()
    },
    async getDataList() {
        let params = {
            imei: this.data.deviceInfo.imei,
            spaceId: this.data.deviceInfo.spaceId
        }
        let {
            deviceType
        } = this.data.deviceInfo
        if (deviceType === "DoorMagnetism") {
            // 门磁
            const res = await reqDoorList(this.data.page, 10, params)
            if (res.code !== 20000) return showMsg(res.message)
            let {
                rows
            } = res.data
            if (this.data.page === 1) {
                this.setData({
                    dataList: rows,
                    isNextPage: true
                })
            } else if (this.data.page > 1 && rows.length) {
                this.setData({
                    dataList: this.data.dataList.concat(rows),
                    isNextPage: true
                })
            } else if (this.data.page > 1 && !rows.length) {
                showMsg('已经到底啦~')
                this.setData({
                    isNextPage: false
                })
            }
        } else if (deviceType === "Infrared") {
            // 红外
            const res = await reqInfraredList(this.data.page, 10, params)
            if (res.code !== 20000) return showMsg(res.message)
            let {
                rows
            } = res.data
            if (this.data.page === 1) {
                this.setData({
                    dataList: rows,
                    isNextPage: true
                })
            } else if (this.data.page > 1 && rows.length) {
                this.setData({
                    dataList: this.data.dataList.concat(rows),
                    isNextPage: true
                })
            } else if (this.data.page > 1 && !rows.length) {
                showMsg('已经到底啦~')
                this.setData({
                    isNextPage: false
                })
            }
        } else {
            return
        }
    },
    // 滚动到底部触发
    lower() {
        if (!this.data.isNextPage) return
        this.setData({
            page: this.data.page + 1,
        })
        this.getDataList()
    },
    // 下拉刷新
    async onRefresh() {
        this.setData({
            dataList: [],
            page: 1,
            isNextPage: true,
            triggered: true,
        })
        await this.getDataList()
        this.setData({
            triggered: false,
        })
    },
    // 操作列表后回到最初状态
    // getInitDataList() {
    //     this.setData({
    //         page: 1,
    //         isNextPage: true,
    //         dataList: []
    //     })
    //     this.getDataList()
    // },

    // 下发指令开始
    goAlarmDetail() {
        if (this.data.deviceInfo.deviceType == 'SmokeAlarm') {
            this.setData({
                serviceType: "muffling_cmd",
                serviceValue: 1,
                serviceTypename: "消音下发",
                serviceValuename: "消音",
                show: true,
            })
        } else if (this.data.deviceInfo.deviceType == 'Infrared') {
            this.setData({
                serviceType: "work_mode",
                serviceValue: "0",
                serviceTypename: "工作模式",
                serviceValuename: "普通模式",
                show: true,
            })
        }
    },
    onClose() {
        this.setData({
            show: false,
        });
    },
    async bindtapShoudon() {
        if (this.data.deviceInfo.deviceId == '') return showMsg('此设备暂不支持下发指令')
        if (this.data.serviceValue == '') return showMsg('服务值不能为空')
        const res = await reqCommand({
            deviceId: this.data.deviceInfo.deviceId,
            imei: this.data.deviceInfo.imei,
            serviceType: this.data.serviceType,
            serviceValue: this.data.serviceValue,
        })
        if (res.code !== 20000) return showMsg(res.message)
        console.log(res);
        this.setData({
            show: false,
        });
        showMsg('下发指令成功！')
    },
    Clickstatus() {
        this.setData({
            showStatus: true
        })
    },
    Clickstatus2() {
        this.setData({
            showStatus2: true
        })
    },
    statusConfirm(e) {
        const {
            value,
        } = e.detail;
        console.log(value);
        this.setData({
            "serviceType": value.id,
            "serviceTypename": value.text,
            showStatus: false,
        })
        if (value.id == 'temperature_throshold' || value.id == 'heartbeat_time') {
            this.setData({
                serviceValue: ""
            })
        } else if (value.id == 'muffling_cmd') {
            this.setData({
                serviceValue: "1"
            })
        } else if (value.id == 'work_mode') {
            this.setData({
                serviceValue: "0"
            })
        }
    },
    statusClose() {
        this.setData({
            showStatus: false
        })
    },
    statusConfirm2(e) {
        const {
            value,
        } = e.detail;
        this.setData({
            "serviceValue": value.id,
            "serviceValuename": value.text,
            showStatus2: false,
        })
    },
    statusClose2() {
        this.setData({
            showStatus2: false
        })
    },
})